using System;
using System.Collections.Generic;
using System.Text;
using Pg.BioMedics.RatObserver.Database.Model;
using Pg.BioMedics.RatObserver.Util;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using FirebirdSql.Data.Isql;
using FirebirdSql.Data;
using FirebirdSql.Data.FirebirdClient;

namespace Pg.BioMedics.RatObserver.Database.DAO.Impl
{
    class AnalizeResultDAO : BaseDAO, IAnalizeResultDAO
    {
        public void saveAnalizeResult(AnalizeResult ar)
        {
            FbCommand cmd = getConnection().CreateCommand();
            cmd.CommandText = "INSERT INTO ANALIZE_RESULT (USER_DESCRIPTION, ANALIZE_TIME, SPIN_COUNT, SPIN_DIRECTION, PAW_MOVE_COUNT, PAW_MOVE_AMP, VIDEO_INFO_FKEY, SYSTEM_VERSION) VALUES (@userDescription, @analizeTime, @spinCount, @spinDirection, @pawMoveCount, @pawMoveAmp, @videoInfoFKey, @systemVersion)";
            cmd.Parameters.Add("@userDescription", FbDbType.VarChar).Value = ar.userDescription;
            cmd.Parameters.Add("@analizeTime", FbDbType.TimeStamp).Value = ar.analizeTime;
            cmd.Parameters.Add("@spinCount", FbDbType.Integer).Value = ar.spinCount;
            cmd.Parameters.Add("@spinDirection", FbDbType.Char).Value = ar.spinDirection;
            cmd.Parameters.Add("@pawMoveCount", FbDbType.Integer).Value = ar.pawMoveCount;
            cmd.Parameters.Add("@pawMoveAmp", FbDbType.Integer).Value = ar.pawMoveAmp;
            cmd.Parameters.Add("@videoInfoFKey", FbDbType.BigInt).Value = ar.vi.id;
            cmd.Parameters.Add("@systemVersion", FbDbType.VarChar).Value = RatObserver.Util.Version.VERSION;
            cmd.ExecuteNonQuery();
        }

        public void updateAnalizeResult(AnalizeResult ar)
        {
            FbCommand cmd = getConnection().CreateCommand();
            cmd.CommandText = "UPDATE ANALIZE_RESULT SET USER_DESCRIPTION=@userDescription, ANALIZE_TIME=@analizeTime, SPIN_COUNT=@spinCount, SPIN_DIRECTION=@spinDirection, PAW_MOVE_COUNT=@pawMoveCount, PAW_MOVE_AMP=@pawMoveAmp, SYSTEM_VERSION=@systemVersion WHERE VIDEO_INFO_FKEY=@videoInfoFKey";
            cmd.Parameters.Add("@userDescription", FbDbType.VarChar).Value = ar.userDescription;
            cmd.Parameters.Add("@analizeTime", FbDbType.TimeStamp).Value = ar.analizeTime;
            cmd.Parameters.Add("@spinCount", FbDbType.Integer).Value = ar.spinCount;
            cmd.Parameters.Add("@spinDirection", FbDbType.Char).Value = ar.spinDirection;
            cmd.Parameters.Add("@pawMoveCount", FbDbType.Integer).Value = ar.pawMoveCount;
            cmd.Parameters.Add("@pawMoveAmp", FbDbType.Integer).Value = ar.pawMoveAmp;
            cmd.Parameters.Add("@videoInfoFKey", FbDbType.BigInt).Value = ar.vi.id;
            cmd.Parameters.Add("@systemVersion", FbDbType.VarChar).Value = RatObserver.Util.Version.VERSION;
            cmd.ExecuteNonQuery();
        }


        public AnalizeResult getAnalizeResult(VideoInfo vi)
        {
            AnalizeResult ar = null;
            FbCommand cmd = getConnection().CreateCommand();
            cmd.CommandText = "select ID, USER_DESCRIPTION, ANALIZE_TIME, SPIN_COUNT, SPIN_DIRECTION, PAW_MOVE_COUNT, PAW_MOVE_AMP, SYSTEM_VERSION FROM ANALIZE_RESULT WHERE VIDEO_INFO_FKEY = @videoInfoFKey";
            cmd.Parameters.Add("@videoInfoFkey", FbDbType.BigInt).Value = vi.id;
            IDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ar = new AnalizeResult();
                ar.id = reader.GetInt64(0);
                ar.userDescription = reader.GetString(1);
                ar.analizeTime = reader.GetDateTime(2);
                ar.spinCount = reader.GetInt32(3);
                ar.spinDirection = reader.GetChar(4);
                ar.pawMoveCount = reader.GetInt32(5);
                ar.pawMoveAmp = reader.GetInt32(6);
                ar.systemVersion = reader.GetString(7);
                ar.vi = vi;
            }
            return ar;
        }
    }
}
